import Text from "../../framework/common/components/Text";
import { diffStyle } from "../../framework/common/utils/diffStyle";
import clsx from "clsx";
import eventBus from "../../framework/common/eventBus";
import { useEffect } from "react";
import { useUpdate } from "ahooks";
import Radio from "../../framework/common/components/Radio";
import { InjectProvider } from "../../framework/common/context/inject-context";
import Select from "../../framework/common/components/Select";
import SelectCity from "../../framework/common/components/SelectCity";
import Person from "../../framework/common/components/Person";
import Calculation from "../../framework/common/components/Calculation";
import { useForm } from "../../framework/common/context/metadata-context";
import Org from "../../framework/common/components/Org";
import DataConnect from "../../framework/common/components/DataConnect";


//表单标题
export default function ({ title, titleObj = {}, titleStyle }) {
  const {data:tplData,mode}=useForm().root;
  const isPrint=(mode=="Print")
  
  const updater = useUpdate();
  const theStyle = diffStyle(titleStyle, "formStyle");
  let theTitle = title;
  let codePath = "form.form_data.main." + titleObj.code;
  useEffect(() => {
    const listener = () => {
      //更新变化
      updater();
    };
    //监听关联的主表字段变化
    let topic = `field.change.${codePath}`;
    if (titleObj.isCode && titleObj.code) {
      eventBus.subscribe(topic, listener);
    }
    return () => {
      if (titleObj.isCode && titleObj.code) {
        eventBus.unSubscribeEvent(topic, listener);
      }
    };
  }, [codePath]);

  if (titleObj.isCode && titleObj.code) {
    let rangePath = "form.data_range.main." + titleObj.code;
    let sourcePath = "form.form_data.fieldSources.main." + titleObj.code;
    if (titleObj.type === "radio") {
      theTitle = <Radio field={codePath} isSubTableField={false} rangePath={rangePath} sourcePath={sourcePath}></Radio>;
    } else if (titleObj.type === "dropdown") {
      theTitle = <Select field={codePath} isSubTableField={false} rangePath={rangePath} sourcePath={sourcePath}></Select>;
    }
    else if( titleObj.type ==="select_city"){
      theTitle = <SelectCity field={codePath} isSubTableField={false} rangePath={rangePath} sourcePath={sourcePath}></SelectCity>;
    }
    else if(titleObj.type ==="select_person"){
      theTitle = <Person field={codePath} isSubTableField={false} rangePath={rangePath} sourcePath={sourcePath}></Person>;
    }
    else if(titleObj.type ==="calculation"){

      let cell= tplData.form.data_define.main.fields.find(f=>f.code===titleObj.code);
      if(cell)
      {
        theTitle = <Calculation cell={cell} field={codePath} isSubTableField={false}></Calculation>;
      }
     
    }
    else if(titleObj.type ==="select_org"){

      let cell= tplData.form.data_define.main.fields.find(f=>f.code===titleObj.code);
      if(cell)
      {
        theTitle = <Org cell={cell} field={codePath} ></Org>;
      }
     
    }
    // else if(titleObj.type ==="data_connect"){

    //   let cell= tplData.form.data_define.main.fields.find(f=>f.code===titleObj.code);
    //   if(cell)
    //   {
    //     theTitle = <DataConnect cell={cell} field={codePath} isSubTableField={isSubTableField} groupCode={groupCode} rowIndex={rowIndex}></DataConnect>;
    //     //   break;
    //   }
     
    // }
    else {

      theTitle = <Text field={codePath}></Text>;
    }
  }
  return (
    <InjectProvider value={{ isEditable: false }}>
      <div className={clsx("title-text text-inherit  min-h-[40px] leading-40px -mb-1 ", theStyle)}>{theTitle}</div>
    </InjectProvider>
  );
}
